package com.lg.erp.controller.ligen;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.SCompany;
import com.lg.erp.entity.SUser;
import com.lg.erp.entity.yqy.TProduct;
import com.lg.erp.service.ISCompanyService;
import com.lg.erp.service.IUserService;
import com.lg.erp.service.ligen.ISupplierService;
import com.lg.erp.service.ligen.LGProductService;
import com.lg.erp.service.yqy.ITProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lg
 * @since 2021-04-22
 */
@Controller
public class SupplierController {


    @Autowired
    private ISCompanyService companyService;
    @Autowired
    private IUserService userService;
    @Autowired
    private LGProductService lgProductService;

    private static SCompany company;

    private static  SUser one;

    @ResponseBody
    @RequestMapping("/supplier/{current}")
    public JsonResult getSupplierByPage(@PathVariable Integer current){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Page<SCompany> page=new Page<>(current,10);
        IPage<SCompany> pages=null;
        if(principal instanceof User) {
            User user = (User) principal;
            one = userService.getOne(new QueryWrapper<SUser>().lambda()
                    .eq(SUser::getUsername, user.getUsername())
            );
            pages=companyService.getSupplierByPageAndCompanyId(page,one.getCompanyId());
            System.out.println(pages.toString());
        }
        return new JsonResult(1,"ok",pages);
    }
    @ResponseBody
    @RequestMapping("/supplier/{keyWord}/{province}/{city}/{area}/{current}")
    public JsonResult findCompanyByKeyWordAndPage(@PathVariable String keyWord
            ,@PathVariable String province
            ,@PathVariable String city
            ,@PathVariable String area
            ,@PathVariable Integer current){
        Page<SCompany> page=new Page<>(current,10);
        IPage<SCompany> pages=companyService.getSupplierBySupplierNameAndPage((keyWord.equals(" ")?"":keyWord)
                ,(province.equals(" ")?"":province),(city.equals(" ")?"":city),(area.equals(" ")?"":area),page,one.getCompanyId());
        return new JsonResult(1,"ok",pages);
    }

    @RequestMapping("/supplier/company/{id}")
    public String goCompanyById(@PathVariable String id){
        company = companyService.getById(id);
        return "supplier/supplier-index";
    }
    @ResponseBody
    @PostMapping("/supplier")
    public JsonResult getSupplier(){
        return new JsonResult(1,"ok",company);
    }

    @ResponseBody
    @RequestMapping("/supplier/products/{current}")
    public JsonResult getProductBySupplierAndPage(@PathVariable("current") Integer current){
        Page<TProduct> page=new Page<>(current,10);
        IPage<TProduct> productByPageAndCompanyId = lgProductService.getProductByPageAndCompanyId(page, company.getId());
        return new JsonResult(1,"ok",productByPageAndCompanyId);
    }
}
